<template>
  <div>
    <role-manager-table ref="rmt" :type="1" :menuId="this.$route.query.id"
                           v-on:listenToChildNewButtonEvent="newButtonEventFromChild"
                           v-on:listenToChildEditButtonEvent="editButtonEventFromChild"
                           v-on:roleToUser="roleToUser"
                           v-on:goToPermissions="goToPermissions"
                           v-on:showRoleAddUser="showRoleAddUser"
                           v-show="isRole"></role-manager-table>
    <roleUserTable ref="rut" :menuId="this.$route.query.id" v-show="!isRole" v-on:userToRole="userToRole"v-on:showRoleAddUser="showRoleAddUser"></roleUserTable>
    <role-manager-dialog ref="rmd" v-on:listenToChildRefreshEvent="refreshEventFromChild"></role-manager-dialog>
    <roleManagerPermissions ref="rmp"></roleManagerPermissions>
    <role-user-add-table ref="ruat" v-on:refreshRoleUserTable="refreshRoleUserTable"></role-user-add-table>
  </div>
</template>

<script>
  import roleManagerTable from '@/view/roleManager/roleManagerTable.vue'
  import roleUserTable from '@/view/roleManager/roleUserTable.vue'
  import roleManagerDialog from '@/view/roleManager/roleManagerDialog.vue'
  import store from '@/store'
  import roleManagerPermissions from '@/view/roleManager/roleManagerPermissions.vue'
  import roleUserAddTable from '@/view/roleManager/roleUserAddTable.vue'
  export default {
    components: {
      'role-manager-table': roleManagerTable,
      'role-manager-dialog': roleManagerDialog,
      'roleUserTable': roleUserTable,
      'roleManagerPermissions': roleManagerPermissions,
      'roleUserAddTable': roleUserAddTable
    },
    data () {
      return {
        'currentRole': {},
        'refresh': '',
        isRole: true
      }
    },
    methods: {
      showMsgFromChild (data) {
        store.commit('setRoleId', data.id)
        store.commit('setRoleName', data.label)
        this.currentRole = data
      },
      refreshTree () {
        this.refresh = Date.now()
      },
      showTableFromChild () {
        this.refresh = Date.now()
      },
      // 编辑用户（fromChild）
      editButtonEventFromChild (data) {
        this.$refs.rmd.modifyRoleDialog(data)
      },
      // 新建用户（fromChild）
      newButtonEventFromChild () {
        this.$refs.rmd.createRoleDialog()
      },

      roleToUser (row) {
        this.isRole = false
        this.$refs.rut.refreshRoleUserTable(row)
      },
      refreshRoleUserTable (row) {
        this.$refs.rut.refreshRoleUserTable(row)
      },
      userToRole () {
        this.isRole = true
      },
      // 角色添加权限
      goToPermissions (row) {
        this.$refs.rmp.openDialog(row)
      },
      // 添加用户列表
      showRoleAddUser (row) {
        this.$refs.ruat.openDialog(row)
      },
      // 刷新table
      refreshEventFromChild () {
        this.$refs.rmt.getAllDatas({currentPage: 1, pageSize: 10})
      }
    }
  }
</script>
